var express = require('express');
var fs = require('fs');
var app = express();

//设置跨域访问
app.all('*',  (req, res, next)=> {
    res.header("Access-Control-Allow-Origin", "*");
    res.header("Access-Control-Allow-Headers", "X-Requested-With");
    res.header("Access-Control-Allow-Methods", "PUT,POST,GET,DELETE,OPTIONS");
    res.header("X-Powered-By", ' 3.2.1');
    res.header("Content-Type", "application/json;charset=utf-8");
    next();
});

app.get('/index', (req, res)=> {
    fs.readFile('./books.json', (err, data) => {
        var books = []
        if (!err) {
            books = JSON.parse(data)
        };
        res.status(200),
        res.json({
            code: 1,
            data: {
                activeDate:'12月15日-12月25日',
                books: books
            }
        })
      })
});

app.post('/postAddress',(req, res)=> {
    console.log(req);
    res.status(200)
    res.json({
        code: 1
    })
})


//配置服务端口
var server = app.listen(12345)